package com.tesis_2.UI;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.util.ArrayList;

import org.json.JSONException;

import com.example.tesis_2.R;
import com.example.tesis_2.R.layout;
import com.example.tesis_2.R.menu;
import com.tesis_2.BL.BLComentarios;
import com.tesis_2.BL.BLReservas;
import com.tesis_2.BL.BLSeguridad;
import com.tesis_2.MO.MOComentario;
import com.tesis_2.MO.MODscore;
import com.tesis_2.MO.MOPlato;
import com.tesis_2.MO.MOReserva;
import com.tesis_2.Utils.UTDscoresAdapter;
import com.tesis_2.Utils.UTListViewUtility;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RatingBar;

public class UIComentar extends Activity {
	
	Integer rReserv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_uicomentar);
		Bundle b = getIntent().getExtras();
		rReserv = b.getInt("resid");
		ArrayList<MODscore> lsDscores = new ArrayList<MODscore>();
		lsDscores = BLComentarios.getDscoresByResId(rReserv);
		
		//rReserv = rReserv;
		
		//ArrayAdapter<MODscore> multiListAdapter = ;
		UTDscoresAdapter adapter = new UTDscoresAdapter(this,lsDscores);
		((ListView) findViewById(R.id.lstPlatos_comentar)).setAdapter(adapter);
		UTListViewUtility.setListViewHeightBasedOnChildren(((ListView) findViewById(R.id.lstPlatos_comentar)));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_uicomentar, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    
		Intent intent; 
		
		switch (item.getItemId()) 
	    {
	        case R.id.optGuardar_comentar:
	        	SaveComment();
	        break;
	   
	        default: 
	        	
	        break;
	    }
	    return false;
	} 
	
	public void SaveComment()
	{
		
		
		String comment = (((EditText) findViewById(R.id.txtComentario_comentar)).getText().toString());
		int rate = (int) (((RatingBar) findViewById(R.id.rbRate_comentar)).getRating());
		int rrest = (int) (((RatingBar) findViewById(R.id.rbRateRest_comentar)).getRating());
		int rlocal = (int) (((RatingBar) findViewById(R.id.rbRateLocal_comentar)).getRating());
		
		comment.replace(" ", "%20");
		
		@SuppressWarnings("unused")
		ArrayList<MODscore> lsDscores = getDscores();
		
		MOComentario objComentario =  new MOComentario();
		MODscore objDscore = new MODscore();
		objComentario.setRate( (float) rate);
		objComentario.setRrest(rrest);
		objComentario.setRlocal(rlocal);
		objComentario.setCmt(comment.replace(" ", "%20"));
		
		try {
			objComentario = BLComentarios.updateComentario(rReserv, objComentario);
			for (int i= 0; i < lsDscores.size(); i++)
			{
				objDscore = BLComentarios.updateDscore(lsDscores.get(i).getId(), lsDscores.get(i).getRate());
			}
			finish();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	public ArrayList<MODscore> getDscores()
	{
		
		//SparseBooleanArray lsChecked = ((ListView) findViewById(R.id.lsPlatos_reservar)).getCheckedItemPositions();
        ArrayAdapter<MODscore> lsDscoresAdap;
        ArrayList<MODscore> lsDscoresValues = new ArrayList<MODscore>();
        
        lsDscoresAdap = (ArrayAdapter<MODscore>) ((ListView) findViewById(R.id.lstPlatos_comentar)).getAdapter();
        
        for (int i = 0; i < lsDscoresAdap.getCount(); i++) {
            // Item position in adapter
            
            // Add sport if it is checked i.e.) == TRUE!
            lsDscoresValues.add(lsDscoresAdap.getItem(i));
        }
        
        return lsDscoresValues;
		
	}


}
